package com.ruoyi.web.controller.invoice;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.exception.InvoiceBusinessException;
import com.ruoyi.third.base.InvoiceApiResult;
import com.ruoyi.third.entity.request.InputlistExportParam;
import com.ruoyi.third.entity.request.InvoiceCreatePdfParam;
import com.ruoyi.third.entity.request.ReviewDownload;
import com.ruoyi.third.service.ThirdApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;


@Api(tags = "下载")
@RestController
@AllArgsConstructor
@RequestMapping("invoice/download")
public class DownloadController {

    private final ThirdApiService thirdApiService;


    @ApiOperation(value = " 红字信息表下载", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ReviewDownload", value = "ReviewDownload", dataTypeClass = ReviewDownload.class)
    })
    @PostMapping("reviewDownload")
    public InvoiceApiResult reviewDownload(@RequestBody @Validated ReviewDownload param) {
        return thirdApiService.reviewDownload(param);
    }


    @ApiOperation(value = "电子专票下载板式文件 OFD/PDF(返回 url)", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jqbh", value = "机器编号", dataTypeClass = String.class),
            @ApiImplicitParam(name = "downflag", value = "1:ofd 2:pdf 3:预", dataTypeClass = String.class),
            @ApiImplicitParam(name = "fpdm", value = "发票代码", dataTypeClass = String.class),
            @ApiImplicitParam(name = "fphm", value = "发票号码", dataTypeClass = String.class),
    })
    @PostMapping("dzPdfOrDown")
    public InvoiceApiResult dzPdfOrDown(@RequestBody Map<String, String> param) {
        if (StrUtil.isBlank(param.get("jqbh")) || StrUtil.isBlank(param.get("downflag")) || StrUtil.isBlank(param.get("fpdm")) || StrUtil.isBlank(param.get("fphm"))) {
            InvoiceBusinessException.createAndThrowIt("参数错误");
        }
        return thirdApiService.dzPdfOrDown(param.get("jqbh"), param.get("downflag"), param.get("fpdm"), param.get("fphm"));
    }

    @ApiOperation(value = " 生成pdf", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "InvoiceCreatePdfParam", value = "InvoiceCreatePdfParam", dataTypeClass = InvoiceCreatePdfParam.class)
    })
    @PostMapping("createPdf")
    public InvoiceApiResult InvoiceCreatePdf(@RequestBody @Validated InvoiceCreatePdfParam param) {
        return thirdApiService.InvoiceCreatePdf(param);
    }


    @ApiOperation(value = "普票下载PDF", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jqbh", value = "机器编号", dataTypeClass = String.class),
            @ApiImplicitParam(name = "fplxdm", value = "发票类型代码", dataTypeClass = String.class),
            @ApiImplicitParam(name = "ids", value = "ids", dataTypeClass = String.class),
    })
    @PostMapping("pdfDownload")
    public ResponseEntity<byte[]> InvoicePdfDownload(@RequestBody Map<String, String> param) throws IOException {
        if (StrUtil.isBlank(param.get("jqbh")) || StrUtil.isBlank(param.get("fplxdm")) || StrUtil.isBlank(param.get("ids"))) {
            InvoiceBusinessException.createAndThrowIt("参数错误");
        }
        InputStream in = thirdApiService.InvoicePdfDownload(param.get("jqbh"), param.get("fplxdm"), param.get("ids"));
        byte[] body = new byte[in.available()];
        //把字节读取到数组中
        in.read(body);
        //设置请求头
        MultiValueMap<String, String> headers = new HttpHeaders();
        headers.add("Content-Disposition", "attchement;filename=" + param.get("fplxdm"));
        //设置响应状态
        HttpStatus statusCode = HttpStatus.OK;
        in.close();
        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
        return entity;//返回
    }


    @ApiOperation(value = "普票下载OFD", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jqbh", value = "机器编号", dataTypeClass = String.class),
            @ApiImplicitParam(name = "fpdm", value = "发票代码", dataTypeClass = String.class),
            @ApiImplicitParam(name = "fphm", value = "发票号码", dataTypeClass = String.class),
    })
    @PostMapping("downloadOFD")
    public InvoiceApiResult InvoicePdfDownloadOfd(@RequestBody Map<String, String> param) {
        if (StrUtil.isBlank(param.get("jqbh")) || StrUtil.isBlank(param.get("fpdm")) || StrUtil.isBlank(param.get("fphm"))) {
            InvoiceBusinessException.createAndThrowIt("参数错误");
        }
        return thirdApiService.InvoicePdfDownloadOfd(param.get("jqbh"), param.get("fpdm"), param.get("fphm"));
    }


    @ApiOperation(value = " 进项录入信息导出", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "InputlistExportParam", value = "InputlistExportParam", dataTypeClass = InputlistExportParam.class)
    })
    @PostMapping("inputlistExport")
    public InvoiceApiResult inputlistExport(@RequestBody @Validated InputlistExportParam param) {
        return thirdApiService.inputlistExport(param);
    }


}
